var express = require("express");
var router = express.Router();
var column = require("../../models/admin/column.js");
column = new column();
//添加
router.post("/addColumn" , function(req , res){
  var body = req.body,
      data = {
        id:body._id,
        //栏目名称
        name:body.name,
        //创建时间
        releaseTime:new Date().getTime(),
        //简介
        briefing:body.briefing || "",
        //排序
        sort:body.sort||""
      };
  if(!body.name){
    return res.json({
      state:"error",
      message:"请输入栏目名称"
    });
  };
  if(body._id){
    //执行保存
    column.update(data , function(error , data){
      if(error){
        return res.json({
          state:"error",
          message:error
        });
      }else{
          return res.json({
            state:"ok",
            message:"编辑成功",
            data:data
          });
      };
    });
  }else{
    //执行保存
    column.addColumn(data , function(error , data){
      if(error){
        return res.json({
          state:"error",
          message:error
        });
      }else{
        return res.json({
          state:"ok",
          message:"添加成功",
          data:data
        });
      };
    });
  };
});
//获取列表
router.get("/columnList" , function(req , res){
  column.find({} , function(error , data){
    if(error){
      return res.json({
        state:"error",
        message:error
      });
    }else{
      return res.json({
        state:"ok",
        data:data
      });
    };
  });
});
//获取详情
router.get("/columnEdit" , function(req , res){
  var id = req.query.id;
  if(id){
    column.find({_id:id} , function(error , data){
      if(error){
        return res.json({
          state:"error",
          message:error
        });
      }else{
        return res.json({
          state:"ok",
          data:data[0]
        });
      };
    });
  }else{
    return res.json({
      state:"error",
      message:"傻蛋-id不能为空"
    });
  };
});
//删除
router.post("/columnDel" , function(req , res){
  var data = {
    _id:req.body.id
  };
  if(data._id){
    column.del(data , function(error , data){
      if(error){
        return res.json({
          state:"error",
          message:error
        });
      }else{
        return res.json({
          state:"ok",
          message:data
        });
      };
    });
  };
});
module.exports = router;
